﻿using M.Warehouse.API.Application.Command.InventoryCommand;
using M.Warehouse.Domain;
using M.Warehouse.Repository.WarehouseImpl;
using M.Warehouse.Repository.WarehouseInterface;
using MediatR;

namespace M.Warehouse.API.Application.CommandHandlers.InventoryHandlers
{
    public class TransferShowCommandHandlers : IRequestHandler<TransferShowCommand, List<Inventory>>
    {
        private readonly IInventoryRepository _inventoryRepository;
        public TransferShowCommandHandlers(IInventoryRepository inventoryRepository)
        {
            _inventoryRepository = inventoryRepository;
        }
        public async Task<List<Inventory>> Handle(TransferShowCommand request, CancellationToken cancellationToken)
        {
            var list = (await _inventoryRepository.GetAllAsync()).Where(x => !x.IsDeleted && x.InventoryType == 3);
            if (!string.IsNullOrEmpty(request.InventoryNo))
            {
                list = list.Where(x => x.InventoryNo.Contains(request.InventoryNo));
            }
            if (!string.IsNullOrEmpty(request.FinishedProductNo))
            {
                list = list.Where(x => x.FinishedProductNo.Contains(request.FinishedProductNo));
            }
            if (request.InventoryState != null)
            {
                list = list.Where(x => x.InventoryState == request.InventoryState);
            }
            list = list.OrderByDescending(x => x.CreatedDate);
            return list.ToList();
        }
    }
}
